/*
 * @(#)CommentBO.java $version 2011. 10. 14
 * 
 * Copyright 2011 NHN Corp. All rights Reserved.
 * NHN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.hangame.mbl.bo.comment;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import org.apache.commons.collections15.map.HashedMap;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.hangame.mbl.constant.CommonConst;
import com.hangame.mbl.dao.comment.CommentDAO;
import com.hangame.mbl.model.Comment;
import com.hangame.mbl.model.HspUserInfo;
import com.hangame.mbl.util.DateUtil;
import com.hangame.mbl.util.HspUserInfoUtil;
import com.nhncorp.lucy.web.metadata.Inject;

// TODO: Auto-generated Javadoc
/**
 * The Class CommentBO.
 *
 * @author NHN
 */
public class CommentBO {
	
	/** The comment dao. */
	private CommentDAO commentDAO = null;
	
	/** The log. */
	private static Log log = LogFactory.getLog(CommentBO.class);
	
	/** The date util. */
	private DateUtil dateUtil = new DateUtil();

	/**
	 * Sets the comment dao.
	 * 
	 * @param commentDAO the new comment dao
	 */
	@Inject(beanId = "commentDAO")
	public void setCommentDAO(CommentDAO commentDAO) {
		this.commentDAO = commentDAO;
	}

	/**
	 * Gets the comment list.
	 * 
	 * @param bbsNo the bbs no
	 * @param offset the offset
	 * @param pageSize the page size
	 * @return the comment list
	 */
	public List<Comment> getCommentList(int bbsNo, int offset, int pageSize) {
		try {
			ArrayList<Comment> commentList = new ArrayList<Comment>();
			commentList = (ArrayList<Comment>)commentDAO.getCommentList(bbsNo, offset, pageSize);
			if (commentList != null) {
				for (int i = 0; i < commentList.size(); i++) {
					String registDateType = dateUtil.getCommentDateType(commentList.get(i).getRegistDate());
					commentList.get(i).setRegistDateType(registDateType);
					String content = commentList.get(i).getContent();
					if (StringUtils.isNotEmpty(content)) {
						commentList.get(i).setContent(content.replace("\n", "<br>"));
					}
				}
				Collections.reverse(commentList);
			}
			
			return getHspUserInfo(commentList);
		} catch (Exception e) {
			log.error(e, e);
			return null;
		}
	}

	/**
	 * Write comment.
	 * 
	 * @param bbsNo the bbs no
	 * @param userId the user id
	 * @param content the content
	 * @return the int
	 */
	public int writeComment(int bbsNo, String userId, String content) {
		try {
			return commentDAO.writeComment(bbsNo, userId, content);
		} catch (Exception e) {
			log.error(e, e);
			return -1;
		}
	}

	/**
	 * Delete comment.
	 * 
	 * @param bbsNo the bbs no
	 * @param userId the user id
	 * @param commentNo the comment no
	 * @return the int
	 */
	public int deleteComment(int bbsNo, String userId, int commentNo) {
		try {
			return commentDAO.deleteComment(bbsNo, userId, commentNo);
		} catch (Exception e) {
			log.error(e, e);
			return -1;
		}
	}

	/**
	 * Gets the comment count.
	 * 
	 * @param bbsNo the bbs no
	 * @return the comment count
	 */
	public int getCommentCount(int bbsNo) {
		try {
			return commentDAO.selectCommentCount(bbsNo);
		} catch (Exception e) {
			log.error(e, e);
			return 0;
		}
	}

	/**
	 * Gets the hsp user info.
	 * 
	 * @param commentList the comment list
	 * @return the hsp user info
	 */
	public List<Comment> getHspUserInfo(List<Comment> commentList) {
		if (commentList == null) {
			return commentList;
		}

		Map<String, String> userIdMap = new HashedMap<String, String>();
		for (Comment comment : commentList) {
			String userId = userIdMap.get(comment.getUserId());
			if (userId == null) {
				userIdMap.put(comment.getUserId(), comment.getUserId());
			}
		}

		Vector<String> userIdVector = new Vector<String>();
		List<String> userIdList = new ArrayList<String>(userIdMap.values());

		for (String userId : userIdList) {
			userIdVector.add(userId);
		}

		Map<String, HspUserInfo> hspUserInfoMap = HspUserInfoUtil.getHspUserInfo(userIdMap);

		if (hspUserInfoMap != null) {
			for (int i = 0; i < commentList.size(); i++) {
				HspUserInfo hspUserInfo = hspUserInfoMap.get(commentList.get(i).getUserId());
				if (hspUserInfo != null) {
					commentList.get(i).setUserNickName(hspUserInfo.getNickName());
					commentList.get(i).setUserProfileUrl(CommonConst.MPHOTO_PREFIX_URL + hspUserInfo.getsNo() + CommonConst.MPHOTO_SUFFIX_URL);
				} else {
					commentList.get(i).setUserNickName("");
					commentList.get(i).setUserProfileUrl(CommonConst.MPHOTO_PREFIX_URL + 0 + CommonConst.MPHOTO_SUFFIX_URL);
				}
			}
		}

		return commentList;
	}
}
